package com.mls.fivedistrict.server.impl;

import com.mls.fivedistrict.dao.UserTaskMapper;
import com.mls.fivedistrict.exception.CustomerException;
import com.mls.fivedistrict.pojo.UserTask;
import com.mls.fivedistrict.pojo.UserTaskExample;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by chenxiuxiang on 2018/4/1.
 */
@Service("userTaskService")
public class UserTaskServiceImpl implements UserTaskService {
    @Resource(name = "userTaskMapper")
    UserTaskMapper userTaskMapper;

    @Override
    public int addUserTask(UserTask userTask) {
        int i = userTaskMapper.insertSelective(userTask);
        if (i <= 0) {
            throw new CustomerException("用户任务创建失败");
        }
        return i;
    }

    @Override
    public UserTask getUserTask(Long taskId, Long userId) {
        UserTaskExample userTaskExample = new UserTaskExample();
        UserTaskExample.Criteria criteria = userTaskExample.createCriteria();
        criteria.andTaskIdEqualTo(taskId);
        criteria.andUserIdEqualTo(userId);
        List<UserTask> userTasks = userTaskMapper.selectByExample(userTaskExample);
        if (userTasks.size() > 0) {
            return userTasks.get(0);
        }
        return null;
    }

    @Override
    public int deleteTask(Long taskId, Long userId) {
        UserTaskExample userTaskExample = new UserTaskExample();
        UserTaskExample.Criteria criteria = userTaskExample.createCriteria();
        criteria.andTaskIdEqualTo(taskId);
        criteria.andUserIdEqualTo(userId);
        int i = userTaskMapper.deleteByExample(userTaskExample);
        return i;
    }

}
